package com.haoting.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.haoting.commonutils.JwtUtils;
import com.haoting.commonutils.MD5;
import com.haoting.service.exceptionhandler.BlogException;
import com.haoting.usercenter.entity.Consumer;
import com.haoting.usercenter.entity.vo.LoginVo;
import com.haoting.usercenter.entity.vo.RegisterVo;
import com.haoting.usercenter.mapper.ConsumerMapper;
import com.haoting.usercenter.service.ConsumerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tingting
 * @since 2021-01-08
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        String first = loginVo.getFirst();
        String password = loginVo.getPassword();
        if(StringUtils.isEmpty(first)||StringUtils.isEmpty(password)){
            throw new BlogException(20001,"数据为空");
        }

        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",first)
                .or()
                .eq("username",first);
        //判断手机号是否正确
        Consumer consumer1 = baseMapper.selectOne(wrapper);
        if(consumer1 == null){
            return "1";
        }

        if(!MD5.encrypt(password).equals(consumer1.getPassword())){
           return "2";
        }

        //登录成功，生成token
        String jwtToken = JwtUtils.getJwtToken(consumer1.getId(),consumer1.getNickname());
        return jwtToken;
    }

    @Override
    public String register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String username = registerVo.getUsername();
        String password = registerVo.getPassword();

        if(StringUtils.isEmpty(mobile)
        ||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            throw new BlogException(20001,"数据为空");
        }

        //判断验证码
//        String redisCode = redisTemplate.opsForValue().get(mobile);
//        if(!code.equals(redisCode)){
//            throw new BlogException(20001,"注册失败");
//        }

        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile)
        .or()
        .eq("username",username);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
           return "false";
        }

        Consumer consumer = new Consumer();
        consumer.setMobile(mobile);
        consumer.setNickname(nickname);
        consumer.setPassword(MD5.encrypt(password));
        consumer.setUsername(username);
        consumer.setRole("1");
        consumer.setAvatar("https://consumer-0329.oss-cn-chengdu.aliyuncs.com/avatar/avadefault.jpg");
        baseMapper.insert(consumer);
        return "true";
    }
}
